<?php

	require_once 'Zend/Loader.php';

	Zend_Loader::loadClass('Zend_Gdata');
	Zend_Loader::loadClass('Zend_Gdata_AuthSub');
	Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
	Zend_Loader::loadClass('Zend_Gdata_Docs');

	/**
	 * Load a document node from Google Docs.
	 */
	class GoogleDocsLoader {

		private $client;
		private $docs;
		private $feed;

		/**
		 * Set Authentication.
		 */
		public function login($username, $password) {
			$this->client=Zend_Gdata_ClientLogin::getHttpClient($username, $password, Zend_Gdata_Docs::AUTH_SERVICE_NAME);
			$this->docs=new Zend_Gdata_Docs($this->client);
			$this->feed=$this->docs->getDocumentListFeed();
		}

		/**
		 * Load a document.
		 */
		public function loadDocument($id) {
			$this->client->setUri("https://docs.google.com/feeds/download/documents/export/Export?id=$id&exportFormat=odt");
			$response=$this->client->request()->getBody();

			if (!$response)
				throw new Exception("Unable to load ".$id);

			return $response;
		}
	}